package org.example.judge_common.rank;

import lombok.Data;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 榜单
 */
@Data
public class RankList {


    private String contestId;
    private String contestName;
    private Integer auth;
    private Date sealRankTime; // 封榜开始时间
    //榜单用户
    private List<RankUser> users;


    //排序
    public void sort(){
        users.sort(new Comparator<RankUser>() {
            @Override
            public int compare(RankUser o1, RankUser o2) {
                if(o1.getAc()!=o2.getAc())return o2.getAc()-o1.getAc();
                return o1.getTime()-o2.getTime();
            }
        });
        //排名
        for (int i = 0; i < users.size(); i++) {
            //题目数量相等，罚时相等
            if(i>0&& users.get(i).getAc().equals(users.get(i-1).getAc())&&users.get(i).getTime().equals(users.get(i-1).getTime())){
                users.get(i).setRanking(users.get(i-1).getRanking());
            }else users.get(i).setRanking(i+1);
        }

    }
    public static String getRedisKey(String contestId){
        return contestId+"_ranking";
    }

}
